require 'test_helper'

class IntergrationTesting < ActionController::IntegrationTest


  test "Adding a recipe to a cookbook" do

    #login
  post "/login/login", :user_name=>"Tony", :password=>"Tony"
 assert_response :redirect

  # add a recipe
  get "/recipe/index"
  assert_response :success

  get "/recipe/new"
  assert_response :success

  # add a recipe
  media = Media.new(:created_by => 1, :created_at => Time.now)
  media.rmedia = Recipe.new(:title => "Cilantro Lime Rice", :category => "Rice",
                         :directions => "Mix all ingredients cook untill done", :description => "Cilantro lime rice is very good")
  ing1 = Ingredient.new(:name => "Rice", :amount => 1, :amount_unit=>"cups", :notes=>"uncooked")
  ing2 = Ingredient.new(:name => "Chicken Stock", :amount => 1, :amount_unit=>"cups")
  ing3 = Ingredient.new(:name => "Cilantro", :amount => 0.5, :amount_unit=>"cups")
  ing4 = Ingredient.new(:name => "Lime Juice", :amount => 2, :amount_unit=>"Table spoons")

  media.rmedia.ingredient << ing1
  media.rmedia.ingredient << ing2
  media.rmedia.ingredient << ing3
  media.rmedia.ingredient << ing4

  media.save

  #verify media saved with 4 ingredients

  recipe2 = Recipe.find(media.rmedia_id)

  assert_not_nil(recipe2)
  assert_equal(4, recipe2.ingredient.count)

  # add a cookbook

  post "/cookbooks/create", :cookbook=>{:title=>"Test123", :description=>"Test123"}
   assert_response :success
   
  cookbook = Cookbook.new(:title => "title", :user_id => "1")
  cookbook.save

  cookbook2 = Cookbook.find(cookbook.id)

  assert_not_nil(cookbook2)
  # add a recipe to a cookbook
  get "/recipe/view/#{media.rmedia_id}"
  assert_response :success

    xml_http_request(:put, "/recipe/create_cookbook_recipe", :cookbook_recipe => {:recipe_id => recipe2.id,
                                                           :cookbook_id => cookbook2.id})
  assert_response :success
  # load cookbook make sure recipe is present
    get "/cookbooks/show/#{cookbook2.id}"
    assert_response :success
   end
end